package com.itheima.health.controller.admin;

import com.itheima.health.annotation.MyAnno;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.AliOssUtil;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author zhangmeng
 * @description 套餐控制器
 * @date 2019/9/26
 **/
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload")
    @MyAnno(empower = "IMAGE_UPLOAD")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        log.info("文件上传，name:{},size:{}", multipartFile.getOriginalFilename(), multipartFile.getSize());
        try {
            //原始文件名
            String originalFilename = multipartFile.getOriginalFilename();
            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件的请求路径
            String filePath = aliOssUtil.upload(multipartFile.getBytes(), objectName);
            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
    }

    /**
     * 添加套餐
     * @param setmeal   套餐基本信息
     * @param checkgroupIds 检查组ID列表
     * @return
     */
    @MyAnno(empower = "SETMEAL_ADD")
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal , Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        if(!StringUtils.isEmpty(setmeal.getImg())) {
            String img =setmeal.getImg().replace(qiniuUtils.getUrlPrefix(),"");
            setmeal.setImg(img);
        }
        //RPC调用添加数据
        setMealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @return
     */
    @MyAnno(empower = "SETMEAL_QUERY")
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        //调用DAO层查询并返回
        PageResult pageResult = setMealService.findPage(queryPageBean);
        //构造返回结果并返回
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @MyAnno(empower = "SETMEAL_FINDBYID")
    public Result findById(Integer id) {
        log.info("[套餐-根据id查询]id:{},findById:{}", id);
        return setMealService.findById(id);
    }

    /**
     * 编辑
     *
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @MyAnno(empower = "SETMEAL_EDIT")
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, @RequestParam("checkGroupIds") List<Integer> checkGroupIds) {
        log.info("[套餐-编辑]data:{},edit:{}", setmeal, checkGroupIds);
        return setMealService.edit(setmeal, checkGroupIds);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @MyAnno(empower = "SETMEAL_DELETE")
    @PostMapping("/deleteSetmealById")
    public Result deleteSetmealById(Integer id) {
        log.info("[套餐-根据id删除]data:{},edit:{}", id);
        return setMealService.deleteSetmealById(id);
    }
}
